//
//  ZJPAFNetTool.m
//  FittingTrip
//
//  Created by 赵建平 on 16/3/7.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "ZJPAFNetTool.h"
#import <AFNetworking.h>
#import <Reachability.h>

@implementation ZJPAFNetTool

+ (void)GETNetWithUrl:(NSString *)url
                 body:(id)body
             headFile:(NSDictionary *)header
             response:(ZJPResponseStyle)responseStyle
              success:(void (^)(id result))success
              failure:(void (^)(NSError *error))failure {
    //    1.设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //    2.设置请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    //    3.设置返回数据的类型
    switch (responseStyle) {
        case ZJPJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case ZJPXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case ZJPDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    //    4.设置响应数据的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    //    5.UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    //    6.请求数据
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    
    NSString *cachePath = [NSString stringWithFormat:@"%@/%ld.plist", path, [url hash]];
    
 
        [manager GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            if (responseObject) {

                success(responseObject);
                //  成功归档
                [NSKeyedArchiver archiveRootObject:responseObject toFile:cachePath];

            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            
            if (error) {
                Reachability *WiFi = [Reachability reachabilityForLocalWiFi];
                if ([WiFi isReachableViaWiFi] == 1) {
               //     解档
                id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:cachePath];
               //     回调
                if (responseObject) {
                    success(responseObject);
                }
                failure(error);
                } else {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前网络不好" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alertView show];
                    
                    //     解档
                    id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:cachePath];
                    //     回调
                    if (responseObject) {
                        success(responseObject);
                    }
                }
            }
            
        }];        
}



+ (void)POSTNetWithUrl:(NSString *)url
                 body:(id)body
             bodyStyle:(ZJPRequestStyle)bodyRequestStyle
             headFile:(NSDictionary *)header
             response:(ZJPResponseStyle)responseStyle
              success:(void (^)(id result))success
              failure:(void (^)(NSError *error))failure {
    //    1.设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    2. 设置body体数据类型
    switch (bodyRequestStyle) {
        case ZJPRequestJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case ZJPRequestString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                return parameters;
            }];
            break;
        default:
            break;
    }
    //    3.设置请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    //    4.设置返回数据的类型
    switch (responseStyle) {
        case ZJPJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case ZJPXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case ZJPDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    //    5.设置响应数据的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    //    6.UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    //    7.请求数据
    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error) {
            failure(error);
        }
        
    }];
    
}


@end
